package com.time.tell.the.truth.netty.server;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import lombok.extern.slf4j.Slf4j;
import java.nio.charset.StandardCharsets;

@Slf4j
public class RespondHandler extends ChannelOutboundHandlerAdapter {
  @Override
  public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    String response = (String) msg;
    ByteBuf buffer = ctx.alloc().buffer();
    buffer.writeCharSequence(response, StandardCharsets.UTF_8);
    ctx.writeAndFlush(buffer);
    log.info("send result to client success");
  }
}
